
#include "cmd_queue.h"

static cmd_manage_t  cmd_q;

int cmd_queue_init(void)
{
    cmd_q.len = 0;

    cmd_q.e_head = (slist_t *)psram_malloc(sizeof(slist_t));
	if (cmd_q.e_head == NULL) {
		l_d("e_head null\n");
		return -1;
	}
    slist_init(cmd_q.e_head);
    return 0;
}

int cmd_queue_destroy(void)
{
	int ret = 0;
	slist_t* node;
	cmd_link_t * obj = NULL;

	if(!cmd_q.e_head){
		return -1;
	}
	//***********************************************/
	slist_for_each(node,(cmd_q.e_head))
	{
		obj = slist_entry(node,cmd_link_t,e_tail);
		if(obj)
		{
			slist_remove(cmd_q.e_head, &(obj->e_tail));
            psram_free(obj);
            obj = NULL;
			cmd_q.len--;
		}
	}
	//***********************************************/
	if(cmd_q.e_head){
		psram_free(cmd_q.e_head);
		cmd_q.e_head = NULL;
	}
	//***********************************************/

    return 0;
}

int cmd_queue_recv(cmd_info_t *cmd)
{
    cmd_link_t * obj = NULL;

	if(!cmd_q.e_head){
		return -1;
	}
	//**************************************************
    obj = (cmd_link_t *)psram_malloc(sizeof(cmd_link_t));
    if(obj == NULL){
        l_d("obj err\n");
        return -1;
    }
    memset(obj, 0, sizeof(cmd_link_t));
    slist_init(&(obj->e_tail));
    //**************************************************
    obj->e_attr.id     = cmd->id;
	obj->e_attr.time   = 0;
    //**************************************************
    cmd_q.len++;
    slist_add_tail(cmd_q.e_head,&(obj->e_tail));
    //**************************************************

    return 0;
}

int cmd_queue_send(cmd_info_t * msg)
{
	int ret = 0;
	slist_t* node;
	cmd_link_t * obj = NULL;
	//**************************************************
	if(!cmd_q.e_head){
		return -1;
	}
	//**************************************************
	if(slist_isempty(cmd_q.e_head)){
		return -1;
	}
	//**************************************************
	slist_for_each(node, (cmd_q.e_head))
	{
		obj = slist_entry(node, cmd_link_t, e_tail);
		if(obj){
            memcpy(msg,&obj->e_attr,sizeof(cmd_info_t));
            ret = 1;
            break;
		}
	}
	//**************************************************
	if(ret){
		slist_remove(cmd_q.e_head, &(obj->e_tail));
		cmd_q.len--;

        if(obj){
            psram_free(obj);
            obj = NULL;
        }
	}
	//**************************************************

	return 0;
}

int cmd_queue_trav(void)
{
    int i = 0;
	slist_t* node;
    cmd_link_t * obj = NULL;
	//**************************************************
	if(!cmd_q.e_head){
		return -1;
	}
	//**************************************************
	if(slist_isempty(cmd_q.e_head)){
		return -1;
	}

	slist_for_each(node,(cmd_q.e_head))
	{
		obj = slist_entry(node,cmd_link_t,e_tail);
		if(obj){
			#if 1
			l_d("i:id=%d:%d\n",i,obj->e_attr.id);
			#endif
            i++;
		}
	}

	return 0;
}

int cmd_queue_get_len(void)
{
    l_d("len=%d\n",cmd_q.len);

    return cmd_q.len;
}

